package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.system.mapper.mysql.JyDeptMapper;
import com.jy.adscm.zltx.bean.JyDrawingAuditing;
import com.jy.adscm.zltx.bean.JyDrawingEditInfo;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingAuditingMapper;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingEditInfoMapper;
import com.jy.adscm.zltx.service.JyDrawingAuditingService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 图纸的部门会审表 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2018-11-07
 */
@Service
public class JyDrawingAuditingServiceImpl extends ServiceImpl<JyDrawingAuditingMapper, JyDrawingAuditing> implements JyDrawingAuditingService {

    @Autowired
    JyDrawingEditInfoMapper jyDrawingEditInfoMapper;
    @Autowired
    JyDrawingAuditingMapper jyDrawingAuditingMapper;
    Logger logger = LoggerFactory.getLogger(getClass());

    @Transactional
    @Override
    public void auditingEditAudit(JyDrawingAuditing jyDrawingAuditing, JyUser user) throws AdscmSystemException {
        logger.info("开始审核图纸修订记录...");
        Integer deptId = jyDrawingAuditing.getAuditingDept();
        if (user != null && user.getDeptId() == deptId){
            //证明是本部门的人在审核
        }else {
            logger.error("不能跨部门审核...");
            throw new AdscmSystemException("不能跨部门审核");
        }

        //修改本身状态
        jyDrawingAuditing.setAuditor(user.getId());
        jyDrawingAuditing.setAduditingDate(new Date());

        jyDrawingAuditingMapper.updateById(jyDrawingAuditing);

        //修改申请记录的状态：如果审核结果为不通过，则申请记录的审核结果也为不通过
        //如果这是审核通过，并且是最后一条审核记录且其他记录都是通过，则申请记录的审核结果就为审核通过

        JyDrawingEditInfo jyDrawingEditInfo = jyDrawingEditInfoMapper.selectById(jyDrawingAuditing.getDrawingEditId());

        String drawingIsPublish = jyDrawingEditInfo.getDrawingIsPublish();
        if("1".equals(drawingIsPublish)){
           //图纸已发布，不能审核了
            logger.error("图纸已发布，不能审核...");
            throw new AdscmSystemException("图纸已发布，不能审核");
        }

        //获取所有的审核记录
        Wrapper<JyDrawingAuditing> wrapper = new EntityWrapper<>();
        wrapper.eq("drawing_edit_id",jyDrawingAuditing.getDrawingEditId());
        List<JyDrawingAuditing> jyDrawingAuditings = jyDrawingAuditingMapper.selectList(wrapper);

        int shPass = 0;//审核通过的记录
        int shNotPass = 0;//审核不通过的记录
        int notSh = 0;//还没审核的

        for (JyDrawingAuditing drawingAuditing : jyDrawingAuditings) {
            String auditingResult = drawingAuditing.getAuditingResult();

            if ("1".equals(auditingResult)){
                shPass++;
            }else if("0".equals(auditingResult)){
                shNotPass++;
            }else {
                notSh++;
            }
        }
        if(shNotPass > 0){
            jyDrawingEditInfo.setApprovalResult(4);//审核不通过
        }else {
            if(shPass > 0 && notSh > 0){
                //审核中
                jyDrawingEditInfo.setApprovalResult(2);
            }else if(shPass > 0 && notSh == 0){
                //审核通过
                jyDrawingEditInfo.setApprovalResult(3);
            }
        }
        jyDrawingEditInfoMapper.updateById(jyDrawingEditInfo);

    }
}
